/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.components.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.physics.*
import com.windea.study.kotlin.fxgl.sample.dev.*
import dev.*
import javafx.geometry.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.scene.text.*
import javafx.util.*

/**
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class BBoxSample : GameApplication() {
	private var player: Entity? = null
	
	private var scaleX = 1.0
	private var scaleY = 1.0
	
	private var angle = 0.0
	
	private var t: Text? = null
	
	private enum class Type {
		PLAYER, NPC
	}
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 1280
		settings.height = 720
		settings.title = "BBoxSample"
	}
	
	override fun initInput() {
		getInput().addAction(object : UserAction("Change view") {
			override fun onActionBegin() {
				//player.setView(new EntityView(new Rectangle(40, 30, Color.BLUE)));
			}
		}, KeyCode.F)
		
		getInput().addAction(object : UserAction("Change view 1") {
			override fun onActionBegin() {
				//player.setView(texture("bird.png").toAnimatedTexture(2, Duration.seconds(0.33)).play());
			}
		}, KeyCode.G)
		
		onKey(KeyCode.Q) {
			scaleX += 0.1
			
			player!!.scaleX = scaleX
		}
		
		onKey(KeyCode.E) {
			scaleY += 0.1
			
			player!!.scaleY = scaleY
		}
		
		onKey(KeyCode.Z) {
			scaleX -= 0.1
			
			player!!.scaleX = scaleX
		}
		
		onKey(KeyCode.C) {
			scaleY -= 0.1
			
			player!!.scaleY = scaleY
		}
		
		onKey(KeyCode.R) {
			angle -= 5.0
			
			player!!.rotation = angle
		}
		
		onKey(KeyCode.T) {
			angle += 5.0
			
			player!!.rotation = angle
		}
	}
	
	override fun initGame() {
		
		// entity 1
		
		player = entityBuilder()
			.type(Type.PLAYER)
			.at(100.0, 150.0)
			.viewWithBBox("brick.png")
			.with(CollidableComponent(true), DeveloperWASDControl())
			//.with(new LiftComponent().xAxisSpeedDuration(400, Duration.seconds(1)))
			.with(LiftComponent().yAxisSpeedDuration(150.0, Duration.seconds(3.0)).xAxisSpeedDuration(100.0, Duration.seconds(3.0)))
			//.with(new IntervalPauseComponent(Map.of(LiftComponent.class, Duration.seconds(2))))
			.zIndex(250)
			.buildAndAttach()
		
		player!!.transformComponent.rotationOrigin = Point2D(64.0, 64.0)
		
		player!!.transformComponent.scaleOriginXProperty().setValue(64)
		player!!.transformComponent.scaleOriginYProperty().setValue(64)
		
		// entity 2
		
		entityBuilder()
			.type(Type.NPC)
			.at(100.0, 100.0)
			.viewWithBBox(Rectangle(40.0, 40.0, Color.RED))
			.with(CollidableComponent(true))
			.zIndex(250)
			.buildAndAttach()
		
		// entity 3
		
		entityBuilder()
			.type(Type.PLAYER)
			.at(400.0, 150.0)
			.bbox(HitBox(Point2D(5.0, 5.0), BoundingShape.circle(20.0)))
			.view(texture("enemy1.png").toAnimatedTexture(2, Duration.seconds(1.0)).loop())
			.buildAndAttach()
	}
	
	override fun initPhysics() {
		getPhysicsWorld().addCollisionHandler(object : CollisionHandler(Type.PLAYER, Type.NPC) {
			override fun onCollisionBegin(a: Entity?, b: Entity?) {
				t!!.text = "Collision"
			}
			
			override fun onCollisionEnd(a: Entity?, b: Entity?) {
				t!!.text = "No collision"
			}
		})
	}
	
	override fun initUI() {
		t = Text()
		t!!.translateY = 100.0
		
		getGameScene().addUINode(t!!)
	}
	
	companion object {
		
		@JvmStatic
		fun main(args: Array<String>) {
			GameApplication.launch(args)
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(BBoxSample::class.java, args)
}

